import XEUtils from "xe-utils";
import {
  createEditRender,
  createExportMethod,
  createFormItemRender,
  equalDaterange,
  getCellEditFilterProps,
  getCellLabelVNs,
  getDatePickerCellValue,
  getFilterOns,
  getNativeOns,
  handleConfirmFilter
} from "../pluginUtils";
import {RendererMapOptions} from "vxe-table/types/v-x-e-table/renderer";

const value: RendererMapOptions = {
  renderDefault: createEditRender(),
  renderEdit: createEditRender(),
  renderCell(h, renderOpts, params) {
    return getCellLabelVNs(h, renderOpts, params, getDatePickerCellValue(renderOpts, params))
  },
  renderFilter(h, renderOpts, params) {
    const {column} = params
    const {attrs} = renderOpts
    const nativeOn = getNativeOns(renderOpts, params)
    return [
      h('div', {
        class: 'vxe-table--filter-element-wrapper'
      }, column.filters.map((option, oIndex) => {
        const optionValue = option.data
        return h(renderOpts.name, {
          key: oIndex,
          attrs,
          props: getCellEditFilterProps(renderOpts, params, optionValue),
          on: getFilterOns(renderOpts, params, option, () => {
            // 处理 change 事件相关逻辑
            handleConfirmFilter(params, !!option.data, option)
          }),
          nativeOn
        })
      }))
    ]
  },
  defaultFilterMethod(params) {
    const {option, row, column} = params
    const {data} = option
    const {filterRender: renderOpts} = column
    const {props = {}} = renderOpts
    const cellValue = XEUtils.get(row, column.property)
    if (data) {
      switch (props.type) {
        case 'daterange':
          return equalDaterange(cellValue, data, props, 'yyyy-MM-dd')
        case 'datetimerange':
          return equalDaterange(cellValue, data, props, 'yyyy-MM-dd HH:ss:mm')
        case 'monthrange':
          return equalDaterange(cellValue, data, props, 'yyyy-MM')
        default:
          return cellValue === data
      }
    }
    return false
  },
  renderItem: createFormItemRender(),
  renderItemContent: createFormItemRender(),
  cellExportMethod: createExportMethod(getDatePickerCellValue),
  exportMethod: createExportMethod(getDatePickerCellValue)
}
export default value
